﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using BugNET.Common;
using BugNET.Entities;

namespace BugNET.Providers.DataProviders
{
    public partial class SqlDataProvider
    {
        public override List<Wiki> SaveWikiContent(Wiki wikiEntity, ref int resultValue)
        {
            // Validate Parameters
            if (wikiEntity == null) throw (new ArgumentNullException("wiki Entity"));

            using (var sqlCmd = new SqlCommand())
            {
                AddParamToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                AddParamToSqlCmd(sqlCmd, "@ProjectId", SqlDbType.Int, 0, ParameterDirection.Input, wikiEntity.ProjectId);
                AddParamToSqlCmd(sqlCmd, "@PageId", SqlDbType.Int, 0, ParameterDirection.Input, wikiEntity.WikiPageId);
                AddParamToSqlCmd(sqlCmd, "@WikiHTML", SqlDbType.NVarChar, 0, ParameterDirection.Input, wikiEntity.WikiHTML);
                AddParamToSqlCmd(sqlCmd, "@WikiText", SqlDbType.NVarChar, 0, ParameterDirection.Input, wikiEntity.WikiText);
                AddParamToSqlCmd(sqlCmd, "@WikiCreatorUserName", SqlDbType.NText, 255, ParameterDirection.Input, (wikiEntity.UserCreated == string.Empty) ? DBNull.Value : (object)wikiEntity.UserCreated);

                SetCommandType(sqlCmd, CommandType.StoredProcedure, SP_WIKI_SAVECONTENT);
                              
                var wikiHistoryList = new List<Wiki>();
                ExecuteReaderCmd(sqlCmd, GenerateWikiHistoryAfterSaveFromReader, ref wikiHistoryList);
                resultValue = ((int)sqlCmd.Parameters["@ReturnValue"].Value);

                return wikiHistoryList;
            }
        }

        public override List<Wiki> SaveWikiPageDetails(Wiki wikiEntity, ref int resultValue)
        {
            // Validate Parameters
            if (wikiEntity == null) throw (new ArgumentNullException("wiki Entity"));

            using (var sqlCmd = new SqlCommand())
            {
                AddParamToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                AddParamToSqlCmd(sqlCmd, "@PageId", SqlDbType.Int, 0, ParameterDirection.Input, wikiEntity.WikiPageId);
                AddParamToSqlCmd(sqlCmd, "@ProjectId", SqlDbType.Int, 0, ParameterDirection.Input, wikiEntity.ProjectId);
                AddParamToSqlCmd(sqlCmd, "@PageName", SqlDbType.NVarChar, 0, ParameterDirection.Input, wikiEntity.WikiPageName);
                AddParamToSqlCmd(sqlCmd, "@SortOrder", SqlDbType.NVarChar, 0, ParameterDirection.Input, wikiEntity.SortOrder); 
                AddParamToSqlCmd(sqlCmd, "@WikiCreatorUserName", SqlDbType.NText, 255, ParameterDirection.Input, (wikiEntity.UserCreated == string.Empty) ? DBNull.Value : (object)wikiEntity.UserCreated);

                SetCommandType(sqlCmd, CommandType.StoredProcedure, SP_WIKI_SAVEWIKIPAGEDETAILS);

                var wikiHistoryList = new List<Wiki>();
                ExecuteReaderCmd(sqlCmd, GenerateWikiPageDetailsFromReader, ref wikiHistoryList);
                resultValue = ((int)sqlCmd.Parameters["@ReturnValue"].Value);

                return wikiHistoryList;
            }
        }
        public override List<Wiki> GetWikiContent(int projectId, int wikiPageId , ref string wikiPageName  )
        {
            // Validate Parameters
            if (projectId == null) throw (new ArgumentNullException("projectId can not be null"));
            if (wikiPageId == null) throw (new ArgumentNullException("Wiki Page can not be null"));

            using (var sqlCmd = new SqlCommand())
            {
               
                AddParamToSqlCmd(sqlCmd, "@ProjectId", SqlDbType.Int, 0, ParameterDirection.Input, projectId);
                AddParamToSqlCmd(sqlCmd, "@PageId", SqlDbType.Int, 0, ParameterDirection.Input, wikiPageId);
                AddParamToSqlCmd(sqlCmd, "@PageName", SqlDbType.NVarChar, 300, ParameterDirection.Output, wikiPageName);

                SetCommandType(sqlCmd, CommandType.StoredProcedure, SP_WIKI_GETCONTENT);

                var wikiHistoryList = new List<Wiki>();
                ExecuteReaderCmd(sqlCmd, GenerateWikiHistoryFromReader, ref wikiHistoryList);
                wikiPageName = ((string)sqlCmd.Parameters["@PageName"].Value);

                return wikiHistoryList;
            }
        }

        public override List<Wiki> GetWikiPageDetails(int projectId, string searchText)
        {
            // Validate Parameters
            if (projectId == null) throw (new ArgumentNullException("projectId can not be null"));
           

            using (var sqlCmd = new SqlCommand())
            {
                AddParamToSqlCmd(sqlCmd, "@ProjectId", SqlDbType.Int, 0, ParameterDirection.Input, projectId);
                AddParamToSqlCmd(sqlCmd, "@SearchText", SqlDbType.NVarChar, 0, ParameterDirection.Input, searchText);
                SetCommandType(sqlCmd, CommandType.StoredProcedure, SP_WIKI_GETWIKIPAGES);
                var wikiPageDetails = new List<Wiki>();
                ExecuteReaderCmd(sqlCmd, GenerateWikiPageDetailsFromReader, ref wikiPageDetails);

                return wikiPageDetails;
            }
        }

        /// <summary>
        /// Deletes the issue.
        /// </summary>
        /// <param name="issueId">The issue id.</param>
        /// <returns></returns>
        public override bool DeleteWikiPage(int wikiPageId)
        {
            if (wikiPageId <= Globals.NEW_ID) throw (new ArgumentOutOfRangeException("wiki PageId"));

            try
            {
                using (var sqlCmd = new SqlCommand())
                {
                    AddParamToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                    AddParamToSqlCmd(sqlCmd, "@WikiPageId", SqlDbType.Int, 0, ParameterDirection.Input, wikiPageId);

                    SetCommandType(sqlCmd, CommandType.StoredProcedure, SP_WIKI_DELETEWIKIPAGES);
                    ExecuteScalarCmd(sqlCmd);
                    var returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
                    return (returnValue == 0);
                }
            }
            catch (Exception ex)
            {
                throw ProcessException(ex);
            }
        }

    }
}
